package com.wanmait.recruitment.filter;



import com.wanmait.recruitment.pojo.Company;
import com.wanmait.recruitment.pojo.Userinfo;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebFilter( {"/zhuang/user/*","/manage/actor/*","/manage/director/*","/manage/distrit/*","/manage/type/*"})
public class AdminLoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String path = request.getRequestURI();
        String queryString = request.getQueryString();
       if (path.contains("zhuang/login")&&queryString!=null&&queryString.equals("action=login"))
        {
            chain.doFilter(servletRequest,servletResponse);
            return;
        }

        HttpSession session = request.getSession();
        Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
        Company company = (Company)session.getAttribute("company");
        if (userinfo==null&&company==null)
        {
            response.sendRedirect(request.getContextPath()+"/zhuang/login");
        }
        else
        {
            chain.doFilter(servletRequest,servletResponse);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
